<?php
$ver = 3;
$dbhost = "localhost";
$dbuser = "flightplan";
$dbpass = "flightplan!";
$dbname = "flightplan";
$datasource = "http://www.airport-data.com/world-airports/";
$metarsource = "http://www.flightplanning.navcanada.ca/cgi-bin/Fore-obs/metar.cgi";
$icao = $_GET["icao"]; //@todo fix this parameter reference; its everywhere
$icao1 = $_GET["icao1"];
$icao2 = $_GET["icao2"];
$R = 6367.5;
/*
function checkVersion() {
	// Select db
	mysql_select_db('config') or die(mysql_error());
	$result = mysql_query("SELECT value FROM config WHERE key = 'db_ver'");
	$row = mysql_fetch_assoc($result);
	if ($row['db_ver'] != $ver) {
			die("Wrong DB Version. (DB ver ".$row['db_ver']." PHP ver $ver");
	}
}*/
 
function addAirport($icao, $name, $lat, $lon, $elev, $mag_var, $tz) {
	// Escape User Input to help prevent SQL Injection
	$icao = mysql_real_escape_string($icao);
	$name = mysql_real_escape_string($name);
	$lat = mysql_real_escape_string($lat);
	$lon = mysql_real_escape_string($lon);
	$elev = mysql_real_escape_string($elev);
	$mag_var = mysql_real_escape_string($mag_var);
	$tz = mysql_real_escape_string($tz);
	if ($icao != "" && $name != "" && $lat != "" && $lon != "" && $mag_var != "" && $tz != "") {
		$query = "INSERT INTO airports (ICAO, IATA, name, lat, lon, elev, magvar, tz) VALUES('$icao','".substr($icao, 1)."','$name','$lat','$lon','$elev','$mag_var','$tz')";
		mysql_query($query) or die(mysql_error());
	}
}

function getAirportData($icao) {
	// Escape User Input to help prevent SQL Injection
	$icao = mysql_real_escape_string($icao);
	$query = "SELECT * FROM airports WHERE ICAO = '$icao'";
	$result = mysql_query($query) or die(mysql_error());
	return $result;
}

function getMETAR($icao, $metarsource) {
	// Get METAR data from NAV Canada
		exec("wget -nc -P metar --post-data=\"Stations=" . $icao . "&format=raw&Langue=anglais&Region=can\" "  . $metarsource);
	exec("mv metar/metar.cgi metar/".$icao);
	$result = exec("grep \"METAR \" metar/" . $icao. " -m 1");
	return $result;
}


// Connect to server
mysql_connect($dbhost, $dbuser, $dbpass);
//checkVersion();

// Select db
mysql_select_db($dbname) or die(mysql_error());

if ($icao != "") {
	$result = getAirportData($icao);	
	
	if ($result == false) {
		user_error("Query failed: " . mysql_error() . "<br />\n$query");
	} elseif (mysql_num_rows($result) == 0) {
		exec("wget -nc -P airports " . $datasource . $icao);
		$result = exec("grep Longitude/ airports/" . $icao);
		$pos = strrpos($result, "<br>");
		$lonPos = $pos + 1;
		$co = explode("/", substr($result, $lonPos + 3));
		$lat = substr($co[1], 0, -1);
		$lon = $co[0];
		
		$result = exec("grep \"<h1>\" airports/" . $icao);
		$name = substr($result, 23, -5);
		
		$result = exec("grep \">Elevation\" airports/" . $icao);
		$pos = strpos($result, "tt1\">") + 5;
		$elev = substr($result, $pos, strpos($result, "ft") - $pos);

		$result = exec("grep Magnetic airports/" . $icao);
		$pos = strpos($result, "tt1\">") + 5;
		$mag_var['dir'] = substr($result, $pos, 1);
		$delPos = strpos($result, "&deg;"); 
		$mag_var['deg'] = substr($result, $pos + 2, $delPos - $pos + 2);
		$mag_var['min'] = substr($result, $delPos + 5, strpos($result, "(") - $delPos - 5);
		if ($mag_var['dir'] = "W") {
			$mag_var = $mag_var['deg'] + $mag_var['min']/60;
			$mag_var = -1*$mag_var;
		} else {
			$mag_var = $mag_var['deg'] + $mag_var['min']/60;
		}
	 
		$result = exec("grep UTC airports/" . $icao);
		$tz = substr($result, strpos($result, "UTC") + 3, 2);
		
		addAirport($icao, $name, $lat, $lon, $elev, $mag_var, $tz);
		echo "$icao:".substr($icao,1).":$name:$lat:$lon:$elev:$mag_var:$tz:";
	} else {
		while($query_row = mysql_fetch_assoc($result)) { 
			foreach($query_row as $key => $value) { 
				echo "$value:"; 
			}
		}
	}
	echo getMETAR($icao, $metarsource);
} elseif ($icao1 != "" && $icao2 != "") {		
	$result = getAirportData($icao1);
	$row = mysql_fetch_assoc($result);
	$lat1 = $row['lat'];
	$lon1 = $row['lon'];
	$mag_var = $row['magvar'];
	$result = getAirportData($icao2);
	$row = mysql_fetch_assoc($result);
	$lat2 = $row['lat'];
	$lon2 = $row['lon'];

	// haversine, nm
	$dLat = deg2rad($lat2-$lat1);
	$dLon = deg2rad($lon2-$lon1);
	$lat1 = deg2rad($lat1);
	$lat2 = deg2rad($lat2);
	$lon1 = deg2rad($lon1);
	$lon2 = deg2rad($lon2);
	$a = sin($dLat/2)*sin($dLat/2) + sin($dLon/2)*sin($dLon/2)*cos($lat1)*cos($lat2);
	$c = 2*atan2(sqrt($a),sqrt(1-$a));
	$d = $R*$c/1.852;
	$y = sin($dLon)*cos($lat2);
	$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
	$tb = rad2deg(atan2($y, $x));
	$mb = (360 + $tb - $mag_var) % 360;
	echo "$d:$tb:$mb";
}
 
?>
